package org.karn.example.content_provider;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Browser;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ListView;

public class ContentProviderDemo extends Activity {

    private static final String TAG = "LOMO";

    ContentResolver resolver;
    Cursor cursor;
    String selectionArgs[] = null;
    String selectionClause = null;
    int count;
    ContentValues value;
    MyCursorAdapter mCursorAdapter1;
    SimpleCursorAdapter mCursorAdapter2;
    int[] txtIds;

    ListView mList;

    //data base manager
    DBManager dbm;

    //ArrayList<bookmarItem> bookmarkList;
    ArrayList<String> folders;
    ArrayList<bookmarItem> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mList = (ListView)findViewById(R.id.list);

        Uri BookmarksUri = Browser.BOOKMARKS_URI;

        String[] browserCol = {Browser.BookmarkColumns.URL,
                Browser.BookmarkColumns.FAVICON, Browser.BookmarkColumns.TITLE,
                Browser.BookmarkColumns.DATE};

        //selectionClause = Browser.BookmarkColumns.BOOKMARK + " = ?";
        //String[] args = {"1"};

        resolver = getContentResolver();

        ////////////////////////

        ContentValues values = new ContentValues();
        //values.;
        //value.put(BrowserCon., value)
        
        cursor = resolver.query(BookmarksUri, null, Browser.BookmarkColumns.BOOKMARK +"=1", null, null);
        count = cursor.getCount();
        int columns = cursor.getColumnCount();
        String[] colNames = cursor.getColumnNames();
        for(int i=0; i<count; i++) {
            cursor.moveToPosition(i);
            cursor.getString(1);
            /*cursor.getString(1);
            cursor.getString(2);
            cursor.getString(3);
            cursor.getString(4);
            cursor.getString(5);
            cursor.getString(6);
            cursor.getString(7);
            cursor.getBlob(8);//
            cursor.getBlob(9);
            cursor.getString(10);
            cursor.getString(11);
            cursor.getString(12);*/
        }
        ////////////////////////////

        cursor = resolver.query(BookmarksUri, browserCol, Browser.BookmarkColumns.BOOKMARK +"=1", null, null);
        count = cursor.getCount();

        dbm = new DBManager(this);

        new FetchAsync().execute();
        //txtIds = new int[]{R.id.titleTxt,R.id.urlTxt};
        //bookmarkList = new ArrayList<bookmarItem>();
        //int size = cursor.getColumnCount();

        //mCursorAdapter1 = new MyCursorAdapter(this, cursor);
        //mList.setAdapter(mCursorAdapter1);

        //HtmlClass html = new HtmlClass(this, cursor);
        //html.createBookmarkHtml();

    }


    @Override
    protected void onDestroy ( )
    {
        if(cursor != null)
        {
            cursor.close();
        }
        super.onDestroy();

    }


    class FetchAsync extends AsyncTask<Void, Void, Void>
    {

        //ArrayList<bookmarItem> list;
        ArrayList<String> temp;

        @Override
        protected Void doInBackground (Void... params)
        {
            ContentFetcher cf = new ContentFetcher(cursor, ContentProviderDemo.this, dbm);

            if(dbm.isEmpty())
            {
                cf.getContent();
            }

            list = dbm.getAllRowsAsArrays();
            temp = dbm.getFolders();
            folders = new ArrayList<String>();

            for(int i=0;i<temp.size();i++)
            {
                if(!temp.get(i).equals("-"))
                {
                    folders.add(temp.get(i));
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute (Void result)
        {
            MyListAdapter mListAdapter = new MyListAdapter(ContentProviderDemo.this, list, folders);
            mList.setAdapter(mListAdapter);
        }


    }

    public void onCreateFolder(View v)
    {
        folders.add("folder 1");
        MyListAdapter mListAdapter = new MyListAdapter(ContentProviderDemo.this, list, folders);
        mList.setAdapter(mListAdapter);
    }

}



/*selectionClause = Browser.BookmarkColumns.TITLE + " = ?";
selectionArgs[] = {"Android Developer"};
resolver = getContentResolver();
cursor = resolver.query(BookmarksUri, browserCol, selectionClause, selectionArgs, null);
count = cursor.getCount();*/

/*value = new ContentValues();
value.put(Browser.BookmarkColumns.TITLE, "Android Developer");
value.put(Browser.BookmarkColumns.URL, "www.developer.android.com");
resolver.insert(BookmarksUri, value);*/


/*Log.i(TAG, System.currentTimeMillis()+" ");
for(int i = 0; i < size; i++)
{
    Log.i(TAG, cursor.getColumnName(i));
}*/

/*for(int i = 0; i < count; i++)
{
    String[] colNames = cursor.getColumnNames();
    bookmarItem item = new bookmarItem();
    cursor.moveToPosition(i);
    for(int j = 0; j < size; j++)
    {
        int colIndex = cursor.getColumnIndex(colNames[j]);

        switch(colIndex)
        {
            case 0:
                item.title = cursor.getString(0);
                break;
            case 1:
                item.url = cursor.getString(1);
                break;
            case 2:
                item.favicon = cursor.getBlob(2);
                break;
            case 3:
                item.data = cursor.getString(3);
                break;
        }
    }
    bookmarkList.add(item);
}*/

/*mCursorAdapter2 = new SimpleCursorAdapter(
        this,
        R.layout.list_item,
        cursor,
        browserCol,
        txtIds,
        0
);*/

/*mCursorAdapter1 = new MyCursorAdapter(
        this,
        R.layout.list_item,
        cursor,
        browserCol,
        txtIds,
        0
);
mList.setAdapter(mCursorAdapter1);*/
